package com.bayss.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.service.TransactionRecordService;
import com.bayss.core.service.activity.ObgRechargeActivityService;
import com.bayss.web.uresponse.UserResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员充值管理
 *
 * @author ldl
 */
@Slf4j
@RestController
public class RechargeController extends ApiBaseController {
    @Autowired
    private ObgRechargeActivityService obgRechargeActivityService;
    @Autowired
    private TransactionRecordService transactionRecordService;

    /**
     * 可用的充值活动列表
     */
    @RequestMapping("/recharge/activityList.json")
    public ResultMsg activityList() {
        return ResultMsg.success(obgRechargeActivityService.list());
    }


    /**
     * 充值
     *
     * @param userResponse
     * @return
     */
    @PostMapping("/recharge/recharge.json")
    public ResultMsg recharge(UserResponse userResponse, Integer amount, Integer activityId) {
        try {
            String payInfo = obgRechargeActivityService.recharge(userResponse.getUserInfo(), amount, activityId);
            return ResultMsg.success(payInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "充值失败");
        }
    }


    /**
     * 交易记录查询
     *
     * @param transactionType 交易类型
     * @param startTime       yyyy-MM-dd HH:mm:ss
     */
    @RequestMapping("/transaction/recordList.json")
    public ResultMsg transactionRecordList(UserResponse userResponse, Integer transactionType,String cellphone, String startTime, String endTime,Integer page, Integer pageSize) {
        return ResultMsg.success(transactionRecordService.list(userResponse.getUid(), transactionType, cellphone,startTime, endTime,page,pageSize));
    }

    /**
     *交易记录统计
     * @param userResponse
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/transaction/countRecord.json")
    public ResultMsg countRecord(UserResponse userResponse,  String startTime, String endTime) {
        return ResultMsg.success(transactionRecordService.countRecord(userResponse.getUid(), startTime, endTime));
    }
}
